package com.example.ending_demo.dao;

import com.example.ending_demo.model.Address;
import com.example.ending_demo.utils.DBUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SearchDao {

    //将搜索的数据暂放入searchAddress表中
    public void searchAdd(String searchStr,int user_id){
        List<Address> searchList = null;
        AddressDao addressDao = new AddressDao();
        searchList = addressDao.searchAddress(searchStr,user_id);
        Connection conn = DBUtils.getConnection();
        String sql1 = "TRUNCATE TABLE searchaddress;";
        try {
            Statement statement = conn.createStatement();
            statement.executeUpdate(sql1);
            statement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        for(Address address : searchList) {
            String sql = "insert into searchaddress(user_id,addr_username,addr_phone,addr_email,address,address_id) values(?,?,?,?,?,?)";
            PreparedStatement preStmt = null;
            try {
                preStmt = conn.prepareStatement(sql);
                preStmt.setInt(1, address.getUser_id());
                preStmt.setString(2, address.getAddr_username());
                preStmt.setString(3, address.getAddr_phone());
                preStmt.setString(4, address.getAddr_email());
                preStmt.setString(5, address.getAddress());
                preStmt.setInt(6,address.getAddress_id());
                preStmt.executeUpdate();
                preStmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }


        }
        try {
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    //查询通讯录所有数量
    public Integer queryForPageTotalCount(){

        String sql = "select count(*) from searchaddress";
        Connection conn = DBUtils.getConnection();
        try {
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery(sql);
            if(rs.next()){
                Integer temp = rs.getInt(1);
                DBUtils.release(rs,stat,conn);
                return temp;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return null;
    }

    //查询每一页的数据
    public List<Address> queryForPageItems(Integer begin,Integer pageSize){
        ArrayList<Address> addrList = new ArrayList<>();

        Connection conn = DBUtils.getConnection();
        String sql = "select * from searchaddress limit ?,?";
        try {
            PreparedStatement pStat = conn.prepareStatement(sql);

            pStat.setInt(1,begin);
            pStat.setInt(2,pageSize);
            ResultSet rs = pStat.executeQuery();

            while (rs.next()){
                Address addrObj = new Address();
                addrObj.setUser_id(rs.getInt("user_id"));
                addrObj.setAddr_email(rs.getString("addr_email"));
                addrObj.setAddress_id(rs.getInt("address_id"));
                addrObj.setAddr_phone(rs.getString("addr_phone"));
                addrObj.setAddress(rs.getString("address"));
                addrObj.setAddr_username(rs.getString("addr_username"));
                addrList.add(addrObj);
            }
            DBUtils.release(rs,pStat,conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return addrList;
    }

    //根据通讯录的ID查询通讯录信息
    public Address getAddressByAddressID(int addr_id) throws SQLException {
        Address addrObj = null;
        Connection conn = DBUtils.getConnection();
        String sql = "select * from searchaddress where address_id = ?";
        PreparedStatement pstat = conn.prepareStatement(sql);
        pstat.setInt(1,addr_id);
        ResultSet rs = pstat.executeQuery();

        while (rs.next()){
            addrObj = new Address();
            addrObj.setUser_id(rs.getInt("user_id"));
            addrObj.setAddress_id(rs.getInt("address_id"));
            addrObj.setAddr_username(rs.getString("addr_username"));
            addrObj.setAddr_phone(rs.getString("addr_phone"));
            addrObj.setAddr_email(rs.getString("addr_email"));
            addrObj.setAddress(rs.getString("address"));

        }
        return addrObj;

    }


}
